package com.leon.ikun.ui.fragments.searchResultFragments;

import com.leon.ikun.base.baseFragment.BaseLazyFragment;
import com.leon.ikun.beans.home.HomeRecommendApp;
import com.leon.ikun.beans.home.searchResult.SearchResultVideo;
import com.leon.ikun.databinding.FragmentVideoResultBinding;
import com.leon.ikun.http.BaseUrl;
import com.leon.ikun.http.Condition;
import com.leon.ikun.http.HttpApi;
import com.leon.ikun.http.RetrofitClient;
import com.leon.ikun.ui.adapters.searchResult.SearchResultVideoJisuAdapter;
import com.leon.ikun.ui.widget.loader.PaginationLoader;

import java.util.List;

import io.reactivex.rxjava3.core.Observable;

/**
 * @Author Leon
 * @Time 2022/08/02
 * @Desc
 */
public class SearchResultVideoFragment extends BaseLazyFragment<FragmentVideoResultBinding> {
    private final String keyword;
    private int pageNum = 0;
    private final Condition.VideoOrder videoOrder = null;
    private final Condition.VideoDuration videoDuration = null;
    private final Condition.VideoTids videoTids = null;

    private SearchResultVideoJisuAdapter adapter;
    private HttpApi httpApi;
    private PaginationLoader<SearchResultVideo, HomeRecommendApp.VodInfo> loader;
    @Override
    protected void initView() {
        adapter = new SearchResultVideoJisuAdapter(context);
        httpApi = new RetrofitClient(BaseUrl.JSAPP, context).getHttpApi();
        loader = new PaginationLoader<>(binding.content, adapter);
        loader.setGuide(searchResultVideo -> {
            List<HomeRecommendApp.VodInfo> list = searchResultVideo.getList();
            return list;
        } );
        loader.setUpdateInterface(loadType ->{
            Observable<SearchResultVideo> searchResultVideoJiSu = httpApi.getSearchResultVideoJiSu(keyword, ++pageNum);
            return searchResultVideoJiSu;
        } );
    }

    public SearchResultVideoFragment(String keyword) {
        this.keyword = keyword;
    }

    @Override
    public FragmentVideoResultBinding getViewBinding() {
        return FragmentVideoResultBinding.inflate(getLayoutInflater());
    }


    @Override
    protected void onLazyLoad() {
        loader.firstObtain();
    }

    private void reload() {
        pageNum = 0;
        adapter.removeAll();
        loader.setUpdateInterface(loadType -> httpApi.getSearchResultVideoJiSu(keyword,++pageNum)).obtain();

    }
}
